package logic;

/**
 * Controller for EmployeeSystem running screens and methods
 * 
 * @author XA9
 * 
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import ui.ScreenWB;
import db.BankDatabase;

public class EmployeeApp {

	private ScreenWB screenwb;
	/** boolean userAuthenticated */
	private boolean userAuthenticated;
	/** bankDatabase */
	private BankDatabase bankDatabase;
	/** int currentAccountNumber */
	private int currentAccountNumber;

	public EmployeeApp() {

		userAuthenticated = true;
		currentAccountNumber = 0;
		userAuthenticated = false;
		this.bankDatabase = new BankDatabase();

		screenwb = new ScreenWB();

		/**
		 * Assign ActionListener to screens
		 * 
		 */
		screenwb.getEmployeeLogin().addActionListener(
				new EmployeeLoginActionListener(this));
		screenwb.getAddCustomer().addActionListener(
				new AddCustomerActionListener());
		screenwb.getAddAccount().addActionListener(
				new AddAccountActionListener());
		screenwb.getEmployeeMainMenu().addActionListener(
				new EmployeeMainMenuActionListener());
		screenwb.getHandleCustomers().addActionListener(
				new HandleCustomersActionListener());
		screenwb.getHandleAccounts().addActionListener(
				new HandleAccountsActionListener());

	}

	/**
	 * Method run() showing JPanel EmployeeLogin
	 * 
	 */
	public void run() {
		screenwb.Show(ScreenWB.EMPLOYEELOGIN);
		screenwb.setVisible(true);
	}

	/**
	 * 
	 * ActionListener for JPanel EmployeeLogin
	 * 
	 */
	private class EmployeeLoginActionListener implements ActionListener {
		private EmployeeApp employeeApp;

		public EmployeeLoginActionListener(EmployeeApp instance) {
			this.employeeApp = instance;
		}

		public void actionPerformed(ActionEvent e) {

			this.employeeApp.authenticateUser();

			/**
			 * Shows screen EMPLOYEEMAINMENU if user is authed
			 */
			if (e.getSource() == screenwb.getEmployeeLogin().getBtnLogin()) {

				if (this.employeeApp.isAuthed()) {
					screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);
				} else {
					System.out.println("Wrong login or password");
				}

			}

		}
	}

	/**
	 * 
	 * ActionListener for JPanel EmployeeMainMenu
	 * 
	 */
	private class EmployeeMainMenuActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screenwb.getEmployeeMainMenu()
					.getBtnHandleAccounts()) {

				screenwb.getHandleAccounts().displayAccounts();
				screenwb.getHandleAccounts().setAccountId();
				screenwb.Show(ScreenWB.HANDLEACCOUNTS);

			} else if (e.getSource() == screenwb.getEmployeeMainMenu()
					.getBtnHandleCustomers()) {

				screenwb.getHandleCustomers().displayCustomers();
				screenwb.getHandleCustomers().setAccountId();
				screenwb.Show(ScreenWB.HANDLECUSTOMERS);

			} else if (e.getSource() == screenwb.getEmployeeMainMenu()
					.getBtnLogout()) {
				screenwb.getEmployeeLogin().clearFields();
				screenwb.Show(ScreenWB.EMPLOYEELOGIN);

			}

		}

	}

	/**
	 * 
	 * ActionListener for JPanel AddCustomer
	 * 
	 */
	private class AddCustomerActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screenwb.getAddCustomer().getBtnReturn()) {
				screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);
			} else if (e.getSource() == screenwb.getAddCustomer()
					.getBtnAddCustomer()) {

				bankDatabase.addCustomer(screenwb.getAddCustomer()
						.getTfLoginId(), screenwb.getAddCustomer()
						.getTfPassword(), screenwb.getAddCustomer().getTfCpr(),
						screenwb.getAddCustomer().getTfFirstname(), screenwb
								.getAddCustomer().getTfLastname());
				screenwb.getAddCustomer().clearFields();
				screenwb.showMessage("Customer is now added to system");
				screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);

			}
		}

	}

	/**
	 * 
	 * ActionListener for JPanel AddAccount
	 * 
	 */
	private class AddAccountActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == screenwb.getAddAccount().getBtnReturn()) {
				screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);
			} else if (e.getSource() == screenwb.getAddAccount()
					.getBtnAddAccount()) {

				bankDatabase.addAccount(screenwb.getAddAccount()
						.getTfAccountNumber(), screenwb.getAddAccount()
						.getTfAccountName(), screenwb.getAddAccount()
						.getTfBalance(), screenwb.getAddAccount().getTfRate(),
						screenwb.getAddAccount().getTfLoginId());
				screenwb.getAddAccount().clearFields();
				screenwb.showMessage("Account is now added to system");
				screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);

			}
		}

	}

	/**
	 * 
	 * ActionListener for JPanel HandleCustomers
	 * 
	 */
	private class HandleCustomersActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			String id;
			id = screenwb.getHandleCustomers().getAccountId();

			if (e.getSource() == screenwb.getHandleCustomers()
					.getBtnAddNewCustomer()) {
				screenwb.Show(ScreenWB.ADDCUSTOMER);

			} else if (e.getSource() == screenwb.getHandleCustomers()
					.getBtnDelete()) {
				if (screenwb.getHandleCustomers().getAccountId() == "") {
					screenwb.showMessage("No customer was selected");

					return;

				}
				bankDatabase.deleteAccounts(id);
				bankDatabase.deleteCustomer(id);
				screenwb.showMessage("The selected customer was deleted (incl. associated accounts)");
				screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);

			} else if (e.getSource() == screenwb.getHandleCustomers()
					.getBtnReturn()) {
				screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);
			}
		}

	}

	/**
	 * 
	 * ActionListener for JPanel HandleAccounts
	 * 
	 */
	private class HandleAccountsActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			String id;
			id = screenwb.getHandleAccounts().getAccountId();

			if (e.getSource() == screenwb.getHandleAccounts()
					.getBtnAddInterest()) {

				if (screenwb.getHandleAccounts().getAccountId() == "") {
					screenwb.showMessage("No account was selected");

					return;

				}

				bankDatabase.addInterest(id, screenwb
						.getInput("How many quarters do you wish to add for?"));
				screenwb.getHandleAccounts().setAccountId();
				screenwb.showMessage("Interest was added to the selected account");
				screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);

			} else if (e.getSource() == screenwb.getHandleAccounts()
					.getBtnAddAccount()) {
				screenwb.Show(ScreenWB.ADDACCOUNT);

			} else if (e.getSource() == screenwb.getHandleAccounts()
					.getBtnDelete()) {
				bankDatabase.deleteAccount(id);
				screenwb.showMessage("The selected account was deleted");
				screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);

			} else if (e.getSource() == screenwb.getHandleAccounts()
					.getBtnReturn()) {
				screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);

			} else if (e.getSource() == screenwb.getHandleAccounts()
					.getBtnAddInterestAll()) {

				bankDatabase.addInterestAll(id, screenwb
						.getInput("How many quarters do you wish to add for?"));
				screenwb.showMessage("Interest was added to all accounts");
				screenwb.Show(ScreenWB.EMPLOYEEMAINMENU);

			}
		}

	}

	/**
	 * 
	 * Method authenticateUser checks if user input is valid
	 * 
	 */
	public void authenticateUser() {
		int loginId = 0;
		int password = 0;

		loginId = screenwb.getEmployeeLogin().getLoginIdEmployee();
		password = screenwb.getEmployeeLogin().getPasswordEmployee();

		System.out.println(loginId);
		System.out.println(password);

		System.out.println(this.bankDatabase);

		userAuthenticated = bankDatabase.authenticateUser(loginId, password);

		if (userAuthenticated) {
			currentAccountNumber = loginId;
		} else
			screenwb.showMessage("Invalid account number or PIN. Please try again.");
	}

	/**
	 * 
	 * Returns isAuthed
	 * 
	 * @return boolean userAuthenticated
	 * 
	 */
	public boolean isAuthed() {
		return this.userAuthenticated;
	}

	/**
	 * Returns userAuthenticated
	 * 
	 * @return boolean userAuthenticated = false
	 * 
	 */
	public void logOut() {
		this.userAuthenticated = false;

	}

}